/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MpmProjectCirculatedMemberService
* 项目传阅反馈人员表
*
* 修改纪录
* 2021-07-14 版本：1.0 chengengwei 创建。
* @version 版本：1.0
* @author 作者：chengengwei
* 创建日期：2021-07-14
*/
package com.topscomm.mpm.service.impl;

import com.topscomm.mpm.pojo.MpmProjectCirculatedMemberEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectCirculatedMemberService;
import com.topscomm.mpm.service.impl.auto.MpmProjectCirculatedMemberServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectCirculatedMemberService")
@Scope(value = "singleton")
public class MpmProjectCirculatedMemberService extends MpmProjectCirculatedMemberServiceAuto implements IMpmProjectCirculatedMemberService {

    /**
     * @author: chengengwei
     * @date: 2021-07-15
     * @description:保存项目成员数据
     * @modify:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMemberData(Map<String, Object> paraMap) {
        String sourceid = ConvertUtil.convertToString(paraMap.get("sourceid"));
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle(" {0} = {1} and {2} = '{3}' ", MpmProjectCirculatedMemberEntity.FieldSourceid,
                sourceid, MpmProjectCirculatedMemberEntity.FieldCirculatedcode,
                ConvertUtil.convertToString(paraMap.get(MpmProjectCirculatedMemberEntity.FieldCirculatedcode)));
        // 删除之前可能添加的人员记录
        this.deleteByWhere(whereSql.toString());
        // 处理项目成员前后逗号
        String projectmembercode = ConvertUtil.convertToString(paraMap.get("projectmembercode"));
        paraMap.put("projectmembercode", StringUtil.isEmpty(projectmembercode) ? "" : "," + projectmembercode + ",");
        MpmProjectCirculatedMemberEntity mpmProjectCirculatedMemberEntity = new MpmProjectCirculatedMemberEntity();
        mpmProjectCirculatedMemberEntity.convertFromMap(paraMap);
        mpmProjectCirculatedMemberEntity.setCirculatedname(getSessionUserBean().getUser().getName());
        this.insert(mpmProjectCirculatedMemberEntity);
    }

    /**
     * @author: chengengwei
     * @date: 2021-07-15
     * @description:查询项目成员名单
     * @modify:
     */
    @Override
    public List<Map<String, Object>> queryConfirmMemberList(Map<String, Object> paraMap) {
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle(" {0} = {1} ", MpmProjectCirculatedMemberEntity.FieldSourceid,
                ConvertUtil.convertToString(paraMap.get("id")));
        List<String> fieldsList = new ArrayList<>();
        fieldsList.add(MpmProjectCirculatedMemberEntity.FieldCirculatedcode);
        fieldsList.add(MpmProjectCirculatedMemberEntity.FieldProjectmembercode);
        fieldsList.add(MpmProjectCirculatedMemberEntity.FieldCirculatedname);
        fieldsList.add(MpmProjectCirculatedMemberEntity.FieldProjectmembername);
        return this.queryMapFieldsByWhere(whereSql.toString(), fieldsList);
    }

    /**
     * @author: chengengwei
     * @date: 2021-07-16
     * @description:根据sourceid查询项目成员信息
     * @modify:
     */
    @Override
    public Map<String, Object> loadCirculatedMemberInfoBySourceId(Map<String, Object> paraMap) {
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle(" {0} = {1} and {2} = '{3}' ", MpmProjectCirculatedMemberEntity.FieldSourceid,
                ConvertUtil.convertToString(paraMap.get("sourceid")), MpmProjectCirculatedMemberEntity.FieldCirculatedcode,
                this.getSessionUserBean().getUser().getCode());
        List<Map<String, Object>> maps = this.queryMapByWhere(whereSql.toString());
        if (!maps.isEmpty()) {
            return maps.get(0);
        }
        return null;
    }
}
